package com.feng.behavior
{
	import com.feng.ControllerCenter;
	import com.feng.data.UpgradeType;
	import com.feng.debug.logger;
	import com.feng.model.Player;
	import com.feng.stateAnalysis.ThreeAnalysis;
	import com.feng.utils.ChallengeCompute;

	/**
	 * 防御行为
	 * @author 风之守望者
	 * 2013-3-28
	 */
	public class DefenseBehavior extends BehaviorBase
	{
		/** 防御意志 表	值越大防御意志越强 */
		public var defenseWillTable:Array;

		public function DefenseBehavior(controllerCenter:ControllerCenter)
		{
			super(controllerCenter);
		}

		/**
		 * 增加防御意志
		 * @param upgradeType 目标加成编号
		 * @param value 防御意志值
		 */
		public function addDefenseWill(upgradeType:int, value:int):void
		{
			logger("升级【"+upgradeType+"】的欲望："+value);
			defenseWillTable[upgradeType] += value;
		}

		/** 跟风 升级属性	不想被落下，不想低于平均数 （我只想做个平庸的人） */
		public function follow_suit(priority:Number):void
		{
			logger("跟风 升级属性");

			var addAttack:Number = stateData.avgPlayer.attack - feng.attack;
			var addDefense:Number = stateData.avgPlayer.defense - feng.defense;
			var addSpeed:Number = stateData.avgPlayer.speed - feng.speed;

			if (addAttack)
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, addAttack * priority);
			}
			if (addDefense)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, addDefense * priority);
			}
			if (addSpeed)
			{
				addDefenseWill(UpgradeType.ADD_SPEED, addSpeed * priority);
			}
			if (addAttack > 0 && addDefense > 0 && addSpeed > 0)
			{
				addDefenseWill(UpgradeType.ADD_ALL, (addAttack + addDefense + addSpeed) * priority);
			}
		}

		/** 我要攻击力 (向第一名看齐) */
		public function need_attack(priority:Number):void
		{
			logger("我要攻击力 (向第一名看齐)");

			addDefenseWill(UpgradeType.ADD_ATTACK, (multiplayerAnalysis.attackSortList[0].attack - feng.attack) * priority);
		}

		/** 我要速度 */
		public function need_speed(priority:Number):void
		{
			logger("我要速度 ：玩家数大于2 我要速度第二 ；如果玩家数等于2 我要速度第一");

			addDefenseWill(UpgradeType.ADD_SPEED, (multiplayerAnalysis.speedSortList[0].speed - feng.speed) * priority);
		}

		/** 冷静对待增加防御 */
		public function sober_add_defense(priority:Number):void
		{
			logger("冷静对待增加防御");

			if (stateData.avgPlayer.attackDamage(feng) <= Player.MIN_DAMAGE)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
			}
		}

		public function challenge_mode_defense(priority:Number):void
		{
			//设置单挑计算深度
			ChallengeCompute.maxDepth = ChallengeCompute.CHALLENGE_MAX_DEPTH;

			if (enemyList.length == 1)
			{
				var me:Player = feng.copy();
				var enemy:Player = enemyList[0];

				//计算  我的 最优升级属性
				var bestUpgradeType:int = ChallengeCompute.getBestUpgradeType(me, enemy);
				addDefenseWill(bestUpgradeType, priority);
			}
			else
			{
				logger("作者疯了，两个人以上进行单挑行为！");
			}
		}

		public function three_mode_defense(priority:Number):void
		{
			//相对强者最优升级属性方案
			var powerfulBestUpgradeType:int = ChallengeCompute.getBestUpgradeType(feng, threeStateAnalysis.powerfulEnemy);
			//相对弱者最优升级属性方案
			var punyEnemyBestUpgradeType:int = ChallengeCompute.getBestUpgradeType(feng, threeStateAnalysis.punyEnemy);
			switch (threeStateAnalysis.myState)
			{
				//最强
				case ThreeAnalysis.FORTISSIMO:
					addDefenseWill(powerfulBestUpgradeType, priority);
					break;
				//最弱
				case ThreeAnalysis.WEAKEST:
					//
					addDefenseWill(punyEnemyBestUpgradeType, priority);
					break;
				//中间(比强的弱，比弱的强)
				case ThreeAnalysis.MIDDLE:
					addDefenseWill(powerfulBestUpgradeType, priority);
					break;
				//牵制(比强还强，比弱还弱)
				case ThreeAnalysis.CONTAIN:
					addDefenseWill(punyEnemyBestUpgradeType, priority);
					break;
				default:
					addDefenseWill(powerfulBestUpgradeType, priority);
					break;
			}
		}

		public function four_mode_defense(priority:Number):void
		{
			logger("处理四人模式");
			//只升 攻击与防御
			if (feng.upgrade(UpgradeType.ADD_ATTACK).attack < multiplayerAnalysis.attackSortList[1].attack)
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
			}
			else
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
			}
		}

		public function five_mode_defense(priority:Number):void
		{
			logger("处理五人模式");
			//只升 攻击与防御
			if (feng.upgrade(UpgradeType.ADD_ATTACK).attack > multiplayerAnalysis.attackSortList[1].attack)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
			}
			else
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
			}
		}

		public function six_mode_defense(priority:Number):void
		{
			logger("处理六人模式");
			//只升 攻击与防御
			var attackFeng:Player = feng.upgrade(UpgradeType.ADD_ATTACK);
			if (attackFeng.attack > multiplayerAnalysis.attackSortList[1].attack)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
			}
			else
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
			}
		}

		/**
		 * 以比自己更强一点的玩家为目标增强自己
		 */
		public function more_powerful(priority:Number):void
		{
			//选择合适的目标
			var target:Player = multiplayerAnalysis.powerfulSortList[0].player;
			var value:Number = multiplayerAnalysis.powerfulSortList[0].value;
			for each (var obj:Object in multiplayerAnalysis.powerfulSortList)
			{
				if (value > obj.value && obj.value > 0)
				{
					target = obj.player;
					value = obj.value;
				}
			}
			var bestUpgradeType:int = ChallengeCompute.getBestUpgradeType(feng, target);
			addDefenseWill(bestUpgradeType, priority);
		}

		/** 与攻击最高者同速 */
		public function max_attack_same_speed(priority:Number):void
		{
			var maxAttack:int = 0;
			var minSpeed:int = 0;
			for each (var player:Player in enemyList)
			{
				if (player.attack > maxAttack)
				{
					maxAttack = player.attack;
					minSpeed = player.speed;
				}
				else if (player.attack == maxAttack)
				{
					minSpeed = Math.min(player.speed, minSpeed);
				}
			}

			if (feng.speed < minSpeed - 1)
			{
				addDefenseWill(UpgradeType.ADD_SPEED, priority);
			}
			else if (feng.speed == minSpeed - 1 && multiplayerAnalysis.hpSortList[0].hp < 50)
			{
				addDefenseWill(UpgradeType.ADD_ALL, priority);
			}

		}
	}
}
